﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data;

namespace Stone
{
    public class Settings
    {
        //********* CONSTANTS **********

        //Directory
        public static string Application_Path = Application.StartupPath;
        public static string Img_Path = Application_Path + "\\img";
        public static string Maps_Path = Application_Path + "\\maps";
        public static string Audio_Path = Application_Path + "\\audio";
        public static string Skins_Path = Application_Path + "\\Skins";
        public static string Plugins_Path = Application_Path + "\\Plugins";

        //Files
        public static string DB_Settings_Path = Application_Path + "\\db\\settings.db";
        public static string DB_Osm_Path = Application_Path + "\\db\\osm.db";
        public static string DB_Statistics_Path = Application_Path + "\\db\\statistics.db";
        public static string LOG_Path = Application_Path + "\\Log\\error.txt";
        public static string NMEA_path = Application_Path + "\\nmea.txt";
        public static string NMEA_tmp_logging_path = Application_Path + "\\Tmp\\tmpNMEA.txt";
        public static string BulkSQL_Path = Application_Path + "\\Tmp\\bulkSQL.txt";
        public static string BulkSQL_Step1_Path = Application_Path + "\\Tmp\\step1.txt";
        public static string BulkSQL_Step2_Path = Application_Path + "\\Tmp\\step2.txt";
        public static string Ftp_MapDescriptionTmp_Path = Application_Path + "\\Tmp\\MapDescription.xml";

        //********* VARIABLES **********

        //Skin
        public static string Skin_Name = "Default";
        //public static string Skin_Current_Path = Skins_Path + "\\" + Skin_Name;

        //GPS Antenna
        public static string GPS_ComPort = "COM5";
        public static string GPS_ComPort_Baudrate = "4800";

        public static void LoadSettings()
        {
            DB db_settings = new DB(DB_Settings_Path);
            db_settings.Connect();

            //Skin
            LoadSingleSetting(db_settings, "Skin_Name", ref Skin_Name);
            
            //GPS Antenna
            LoadSingleSetting(db_settings, "GPS_ComPort", ref GPS_ComPort);
            LoadSingleSetting(db_settings, "GPS_ComPort_Baudrate", ref GPS_ComPort_Baudrate);

        }
        public static void SaveSettings()
        {
            DB db_settings = new DB(DB_Settings_Path);
            db_settings.Connect();

            //Skin
            SaveSingleSetting(db_settings, "Skin_Name", Skin_Name);
            
            //GPS Antenna
            SaveSingleSetting(db_settings, "GPS_ComPort", GPS_ComPort);
            SaveSingleSetting(db_settings, "GPS_ComPort_Baudrate", GPS_ComPort_Baudrate);
        }
        public static void SaveSingleSetting(DB db, string key, string value)
        {
            DataTable resTable = db.ExecuteQuery("SELECT * FROM app_settings WHERE key = '" + key + "'");
            string query = "";

            if (resTable.Rows.Count > 0)
                query = "UPDATE app_settings SET value = '" + value + "' WHERE key = '" + key + "'";
            else
                query = "INSERT INTO app_settings (key, value) VALUES ('" + key + "', '" + value + "')";

            db.ExecuteNonQuery(query);
        }
        public static void LoadSingleSetting(DB db, string key, ref string value)
        {
            DataTable resTable = db.ExecuteQuery("SELECT * FROM app_settings WHERE key = '" + key + "'");

            if (resTable.Rows.Count > 0)
            {
                string valueLoaded = resTable.Rows[0]["value"].ToString();
                value = valueLoaded;
            }
        }
    }
}
